package com.jqsoft.launcher.dictionarydb;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jqsoft.launcher.activity.bean.DictionaryBean;

import java.util.ArrayList;
import java.util.List;

/**这个是对已经存在的本地数据字典的操作
 * @author gujunqi
 * date：2014-1-16 
 */
public class DataDictionaryDao {
	private static String path = "/data/data/com.jqsoft.launcher/databases/dictionary.db";
//	private static SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
	/**
	 * 返回数据库有多少条数据
	 * @return
	 */
	public static  int getAllCount(){
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		int count=0;
		if(db.isOpen()){
			Cursor cursor = db.rawQuery("select * from dictionary", null);
			count = cursor.getCount();  
			cursor.close();
			db.close();
		}
		return count;
	}
	
	/**
	 * 获取对应分组集合信息 根据dict_code来查的
	 * @return
	 */
	public static List<DictionaryBean> getGroupList(String dict_code){
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
		List<DictionaryBean> groupLists = new ArrayList<DictionaryBean>();
		DictionaryBean info;
		if(db.isOpen()){
			Cursor cursor = db.rawQuery("select * from dictionary where dict_code=? and iused=1 order by isort asc", new String[]{dict_code});
			while(cursor.moveToNext()){				
				String id = cursor.getString( cursor.getColumnIndex("id"));
				String dict_code2 = cursor.getString( cursor.getColumnIndex("dict_code"));
				int isort = cursor.getInt( cursor.getColumnIndex("isort"));
				String item_name = cursor.getString( cursor.getColumnIndex("item_name"));
				String item_code = cursor.getString( cursor.getColumnIndex("item_code"));
				int iused = cursor.getInt( cursor.getColumnIndex("iused"));
				info=new DictionaryBean(id, dict_code2, isort, item_name, item_code, iused);
				groupLists.add(info);
				info=null;
			}
			cursor.close();
			db.close();
		}
		return groupLists;
	}
	
//	插入
	public static boolean add(String id, String dict_code, int isort, String item_name,
			String item_code, int iused) {
		long result = 0;
//		if (find(functionName + "")) {
//			return false;
//		}
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
		if (db.isOpen()) {
			// db.execSQL("insert into person (name,phone) values ('zhangsan','120')")
			// db.execSQL("insert into person (name,phone) values (?,?)", new
			// Object[]{name,phone});
			ContentValues values = new ContentValues();
			values.put("id", id);
			values.put("dict_code", dict_code);
			values.put("isort", isort);
			values.put("item_name", item_name);
			values.put("item_code", item_code);
			values.put("iused", iused);
			result = db.insert("dictionary", null, values);
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
	}
	
//	清空数据
	public static boolean  deleteAll() {
		long result = 0;
		SQLiteDatabase db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
		if (db.isOpen()) {
//			db.execSQL("delete from dictionary" );
			 result = db.delete("dictionary", null, null);
			//返回值表示受影响的行数
			db.close();
		}
		if (result == -1) {
			return false;
		} else {
			return true;
		}
		
	}
//	
//	/**
//	 * 获取某一个位置的分组名称
//	 * @param groupPosition
//	 * @return
//	 */
//	public static String getGroupNameByPosition(int groupPosition){
//		String name = null;
//		int newposition = groupPosition+1;
//		if(db.isOpen()){
//			Cursor cursor = db.rawQuery("select name from classlist where idx=?", new String[]{newposition+""});
//			if(cursor.moveToFirst()){
//				name = cursor.getString(0);
//			}cursor.close();
//			db.close();
//		}
//		return name;
//	}
//	
//	
//	/**
//	 * 获取每一个分组里面有多少个子孩子
//	 * @param groupPosition
//	 * @return
//	 */
//	public static int getChildrenCount(int groupPosition){
//		int count=0;
//		int newposition = groupPosition + 1;
//		String sql = "select * from table"+newposition;
//		if(db.isOpen()){
//			Cursor cursor = db.rawQuery(sql,null);
//			count = cursor.getCount();
//			cursor.close();
//			db.close();
//		}
//		
//		return count;
//	}
//	
//	/**
//	 * 获取每一个位置的子孩子的信息
//	 */
//	public static String getChildNameByPosition(int groupPosition,int childPosition){
//		String result = null;
//		int newGroupPosition = groupPosition + 1;
//		int newChildPosition = childPosition + 1;
//		String sql = "select name,number from table"+newGroupPosition+" where _id=?";
//		if(db.isOpen()){
//			
//			Cursor cursor =  db.rawQuery(sql,new String[]{newChildPosition+""});
//			if(cursor.moveToFirst()){
//				
//			String name =	cursor.getString(0);
//			String number = cursor.getString(1);	
//			result = name+"\n"+number;
//			}
//			cursor.close();
//			db.close();
//		}
//		
//		return result;
//	}
//	/**
//	 * 获取每一个分组所有的子孩子的信息
//	 */
//	public static List<String> getChildNameByPosition(int groupPosition){
//		String result = null;
//		List<String> results = new ArrayList<String>();
//		int newGroupPosition = groupPosition + 1;
//		String sql = "select name,number from table"+newGroupPosition;
//		if(db.isOpen()){
//			Cursor cursor =  db.rawQuery(sql,null);
//			while(cursor.moveToNext()){
//				
//			String name =	cursor.getString(0);
//			String number = cursor.getString(1);	
//			result = name+"\n"+number;
//			results.add(result);
//			result = null;
//			}
//			cursor.close();
//			db.close();
//		}
//		return results;
//	}
//	
}
